﻿using Jizsoft.JFrame.Result;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jizsoft.JFrame.Modeling.Domain.Services
{
    public class CreateService
    {
        /// <summary>
        /// 
        /// 创建字段数据类型
        /// </summary>
        /// <param name="categoryId"></param>
        /// <param name="code"></param>
        /// <param name="caption"></param>
        /// <param name="dataType"></param>
        /// <param name="minLength"></param>
        /// <param name="maxLength"></param>
        /// <param name="validationRegex"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public Jizsoft.JFrame.Result.ServiceResult CreateFieldType(Guid categoryId, string code, string caption, Guid dataType, int? minLength, int? maxLength, string validationRegex, string description)
        {
            using(JFDbContext dbContext = new JFDbContext())
            {
                Entities.FieldType fieldType = new Entities.FieldType();
                fieldType.Id = Guid.NewGuid();
                fieldType.CategoryId = categoryId;
                fieldType.Code = code;
                fieldType.Caption = caption;
                fieldType.DataType = dataType;
                fieldType.MinLength = minLength;
                fieldType.MaxLength = maxLength;
                fieldType.ValidationRegex = validationRegex;
                fieldType.Description = description;
                fieldType.IsPrivate = false;
                dbContext.Set<Entities.FieldType>().Add(fieldType);
                try
                {
                    dbContext.SaveChanges();
                    return new Result.ServiceResult();
                }
                catch(Exception ex)
                {
                    return new Result.ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult CreateModel(string code, string name, string description)
        {
            using(JFDbContext dbContext = new JFDbContext())
            {
                Entities.Model model = new Entities.Model();
                model.Id = Guid.NewGuid();
                model.Code = code;
                model.Name = name;
                model.Version = 1;
                model.IsDeleted = false;
                model.CreatedTime = DateTime.Now;
                model.CreatorId = "admin";
                model.IsEnabled = true;
                model.IsPrimary = true;
                model.Description = description;
                dbContext.Set<Entities.Model>().Add(model);
                try
                {
                    dbContext.SaveChanges();
                    return new Result.ServiceResult();
                }
                catch (Exception ex)
                {
                    return new Result.ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult CreateModelField(Guid modelId, string name, Guid fieldTypeId, bool isRequired, int sortIndex, string description)
        {
            using (JFDbContext dbContext = new JFDbContext())
            {
                Entities.ModelField field = new Entities.ModelField();
                field.Id = Guid.NewGuid();
                field.Name = name;
                field.ModelId = modelId;
                field.FieldTypeId = fieldTypeId;
                field.SortIndex = sortIndex;
                field.CreatedTime = DateTime.Now;
                field.CreatorId = "admin";
                field.IsRequired = isRequired;
                field.IsEnabled = true;
                field.IsDeleted = false;
                field.Description = description;

                dbContext.Set<Entities.ModelField>().Add(field);
                try
                {
                    dbContext.SaveChanges();
                    return new Result.ServiceResult();
                }
                catch (Exception ex)
                {
                    return new Result.ServiceResult(1, ex.Message);
                }
            }
        }

        /// <summary>
        /// model2关联到model1上，
        /// model2成为model1的部分
        /// </summary>
        /// <param name="model1Id"></param>
        /// <param name="model1Multiplicity"></param>
        /// <param name="model2Id"></param>
        /// <param name="model2Multiplicity"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public ServiceResult CreateModelRelation(Guid model1Id, int model1Multiplicity, Guid model2Id, int model2Multiplicity, int type)
        {
            using (JFDbContext dbContext = new JFDbContext())
            {
                Entities.ModelRelation relation = new Entities.ModelRelation();
                relation.Model1Id = model1Id;
                relation.Model2Id = model2Id;
                relation.Model1Multiplicity = model1Multiplicity;
                relation.Model2Multiplicity = model2Multiplicity;
                relation.Type = type;
                relation.CreatedTime = DateTime.Now;
                relation.CreatorId = "admin";

                dbContext.Set<Entities.ModelRelation>().Add(relation);
                try
                {
                    dbContext.SaveChanges();
                    return new Result.ServiceResult();
                }
                catch (Exception ex)
                {
                    return new Result.ServiceResult(1, ex.Message);
                }
            }
        }
    }
}
